<template>
  <div class="company">
    <div class="docking">
      <div class="dockingBox flex align_center justify_between">
        <div class="docking_left">
          <div class="flex align_center">
            <div>
              <img :src="data.head_img ? data.head_img : logo_z">
            </div>
            <div>
              <p>
                <span class="contenr3 fontBold color1">{{ data.username }}</span>
                <span class="contenr3 font400 color6 leftm">{{ data.job }}</span>
              </p>
              <p class="content3 font400 color3">
                {{ data.company }}
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div
      class="company_content font400 content3 color3"
    >
      <p>电话号码：{{ data.phone }}</p>
      <p>邮箱：{{ data.email }}</p>
      <p>地址：{{ data.address }}</p>
    </div>
    <div
      class="company_content font400 content3 color3"
      v-if="data.desc"
      v-html="data.desc"
    />
  </div>
  <div class="fexd_footer">
    <div class="footer_btn flex align_center justify_between">
      <div
        class="font400 content3 color4"
        @click="back"
      >
        <svg
          class="icon"
          aria-hidden="true"
        >
          <use xlink:href="#icon-fanhui" />
        </svg>
        <p class="content2 font400 color3">
          返回
        </p>
      </div>
      <div
        class="font400 content3 color4"
        @click="share"
      >
        <svg
          class="icon"
          aria-hidden="true"
        >
          <use xlink:href="#icon-fenxiang" />
        </svg>
        <p class="content2 font400 color3">
          分享
        </p>
      </div>
      <div
        @click="userExchange"
        v-if="data.is_friend === 0"
        class="agree font400 content3 color4"
      >
        交换名片
      </div>
      <div
        @click="submit"
        v-if="data.is_friend === 1"
        class="agree font400 content3 color4 ta"
      >
        与TA联系
      </div>
    </div>
  </div>
  <Loading
    :visible="visible"
    :share-img="shareImg"
    @closeLoading="closeLoading"
  />
</template>

<script setup lang="ts">
import { ref, defineAsyncComponent } from 'vue'
import { Toast, Dialog } from 'vant'
import { squareUserDetail, exchange, squareUser } from '@/api'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import logo_z from '@/assets/img/logo-z.jpg'
const Loading = defineAsyncComponent(() => import('@/components/Loading/index.vue'))
const store = useStore()
const router = useRouter()
const route = useRoute()
const data:any = ref([])
const visible: any = ref(false)
const shareImg: any = ref('')

const getData = async () => {
  data.value = await squareUserDetail({ user_id: route.query.id })
}
getData()

const back = () => {
  if (store.state.from.path !== '/') {
    router.back()
  } else {
    router.replace('/')
  }
}

const userExchange = async () => {
  try {
    await store.dispatch('fetchUserInfo')
    if (store.state.userInfo.is_vip) {
      await exchange({ user_id: data.value.id })
      Dialog.alert({
        message: '您的交换名片申请已提交，请耐心等待用户审核',
        confirmButtonColor: '#FF7A1F'
      }).then(() => {
        // on close
      })
    } else {
      Dialog.confirm({
        title: '温馨提示',
        message: '房协会员专属权限，快去申请入会吧',
        confirmButtonText: '去入会',
        cancelButtonText: '暂不入会'
      })
        .then(() => {
          router.replace('/userCenter')
        })
    }
  } catch (error:any) {
    Toast(error.message)
  }
}

const submit = () => {
  window.location.href = 'tel://' + data.value.phone
}

const closeLoading = async () => {
  visible.value = false
}

const share = async () => {
  try {
    visible.value = true
    await squareUser({ personnel_id: route.query.id, web_url: encodeURIComponent(window.location.href) })
    shareImg.value = `${import.meta.env.VITE_API_BASEURL}v1/square_user/share?personnel_id=${route.query.id}&web_url=${encodeURIComponent(window.location.href)}`
  } catch (error:any) {
    Toast(error.message)
  }
}
</script>

<style lang="less" scoped>
.company {
    padding: 0 15px 80px;
    .company_content {
        margin-top: 20px;
        padding: 15px;
        background-color: #fff;
        border-radius: 5px;
        line-height: 25px;
    }
}
.docking {
    width: 100%;
    background: #ffffff;
    border-radius: 10px;
    margin: 15px 0;
    .dockingBox {
        padding: 15px;
        .docking_left {
            img {
                width: 70px;
                height: 70px;
                border-radius: 5px;
                object-fit: cover;
                margin-right: 15px;
            }
            p {
                width: 100%;
            }
            p:last-child {
                margin-top: 10px;
            }
            .leftm {
                margin-left: 10px;
            }
        }
    }
}

.fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
    .footer_btn {
        padding: 0 30px;
        height: 100%;
        text-align: center;
        .agree {
            width: 189px;
            height: 44px;
            background: #ff7a1f;
            border-radius: 5px;
            text-align: center;
            line-height: 44px;
        }
        .ta{
            width: 242px;
        }
        .icon {
            width: 23px;
            height: 23px;
            margin-bottom: 2px;
        }
    }
}
@media (min-width: 750px) {
  .company {
    padding: 0 15px 80px;
    .company_content {
        margin-top: 20px;
        padding: 15px;
        background-color: #fff;
        border-radius: 5px;
        line-height: 25px;
    }
}
.docking {
    width: 100%;
    background: #ffffff;
    border-radius: 10px;
    margin: 15px 0;
    .dockingBox {
        padding: 15px;
        .docking_left {
            img {
                width: 70px;
                height: 70px;
                border-radius: 5px;
                object-fit: cover;
                margin-right: 15px;
            }
            p {
                width: 100%;
            }
            p:last-child {
                margin-top: 10px;
            }
            .leftm {
                margin-left: 10px;
            }
        }
    }
}

.fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
    .footer_btn {
        padding: 0 30px;
        height: 100%;
        text-align: center;
        .agree {
            width: 189px;
            height: 44px;
            background: #ff7a1f;
            border-radius: 5px;
            text-align: center;
            line-height: 44px;
        }
        .ta{
            width: 242px;
        }
        .icon {
            width: 23px;
            height: 23px;
            margin-bottom: 2px;
        }
    }
}
}

</style>
